// Parts Copyright 2008 Newgrove Consultants Limited
// Modified By John Diss 
// From original work:
//
using System.IO;
using GeoAPI.Geometries;

namespace MsSqlSpatial.IO
{
    public static class MsSqlSpatialWriter
    {
        public static void Write(IGeometry geometry, Stream stream)
        {
            byte[] bytes = Write(geometry);
            stream.Write(bytes, 0, bytes.Length);
        }

        public static byte[] Write(IGeometry geometry)
        {
            byte[] bytes = Global.Services.GeometryFactory.WkbWriter.Write(geometry);

            int? srid = Global.ParseSridString(geometry.Srid);

            if (!srid.HasValue)
                return bytes;

            using (var ms = new MemoryStream())
            {
                using (var bw = new BinaryWriter(ms))
                {
                    bw.Write(bytes);
                    bw.Write(srid.Value);
                }
                return ms.ToArray();
            }
        }
    }
}